<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class Transition extends Base
{
    //获取消费记录
    public function getPageList($options=[],$where=[],$whereDates,$wherein){
        try{
            $limit = (isset($options['limit']) && $options['limit']) ? $options['limit'] : 1;
            $page = (isset($options['page']) && $options['page']) ? $options['page'] : 1;
            $model = $this->leftJoin('users','users.id','=','transitions.uid');
            if(!empty($whereDates)){
                $model = $model->whereDate('transitions.created_at','>=',$whereDates[0])->whereDate('transitions.created_at','<',$whereDates[1]);
            }
            if(!empty($wherein)){
                $model = $model->whereIn('transtype',$wherein);
            }
            $model=$model->select('transitions.*','users.username','users.email','users.contact','users.total','users.username_zn')
                ->where($where)->orderBy('transitions.created_at','desc');
            $result = $this->paging($model,$page,$limit);
            return $result;
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //后台餐厅订单支付操作
    public function dinOrdPay($data){
        try {
            $order = DiningReserve::find($data['did']);
            if(!$order){
                return [
                    'code'=>1,
                    'message'=>'订单不存在'
                ];
            }
            $user = User::find($order->uid);
            if($user->deleted_at){
                return [
                    'code'=>1,
                    'message'=>'用户被拉黑，不可交易'
                ];
            }
            if($order->ordtype==2){ //现场点单
                $trans = $this;
                $trans->orderid = $data['did'];
                $trans->uid = $order->uid;
                $ranstr = Str::random(4);
                $transNo = $ranstr.date("YmdHis");
                $trans->transno = $transNo;
                $yinfu = $this->getTransMoney($data['did']); //重新统计未付款金额
                $youhui = (isset($data['decre'])&&$data['decre'])?$data['decre']:0;
                $yinfu = $yinfu - $youhui - $order->earnest; //计算交易金额
                $trans->number = $yinfu;
            }else{ //预定点单
                $trans = $this->where([ ['orderid','=',$data['did']],['transtype','=',$data['transtype']] ])->find();
                if(!$trans){
                    return [
                        'code'=>1,
                        'message'=>'交易单不存在'
                    ];
                }
                if($trans->status!=1){ //如果该交易单不是待交易状态，则不可确认或取消支付
                    return [
                        'code'=>1,
                        'message'=>'该交易单不是待交易状态，不可支付'
                    ];
                }
                if($data['transtype']==1){ //订金支付
                    if($order->status!==0){ //如果订单不是待付款状态
                        return [
                            'code'=>1,
                            'message'=>'该订单不是待付款状态'
                        ];
                    }
                }else{ //结账支付
                    if(!in_array($order->status,[1,2,3])){ //如果订单不是待结账状态
                        return [
                            'code'=>1,
                            'message'=>'该订单状态错误'
                        ];
                    }
                    $yinfu = $this->getTransMoney($data['did']); //重新统计未付款金额
                    $youhui = (isset($data['decre'])&&$data['decre'])?$data['decre']:0;
                    $yinfu = $yinfu - $youhui - $order->earnest; //计算交易金额
                    $trans->number = $yinfu;
                }
            }
            if($data['dotype'] == 1){ //确认支付
                $order->status = 4;
                $trans->status = 2;
                $user->total += $trans->number;//添加累计消费
                if($data['paytype']==1){ //钱包支付
                    if($user->balance < $trans->number){
                        return [
                            'code'=>1,
                            'message'=>'钱包余额不足'
                        ];
                    }
                    $trans->balance = $user->balance - $trans->number;
                    $user->balance -=  $trans->number;
                    $trans->cycle_balance = $user->cycle_balance;
                }elseif ($data['paytype']==2){ //反馈钱包支付
                    if($user->cycle_balance < $trans->number){
                        return [
                            'code'=>1,
                            'message'=>'反馈钱包余额不足'
                        ];
                    }
                    $trans->cycle_balance = $user->cycle_balance - $trans->number;
                    $user->cycle_balance -=  $trans->number;
                    $trans->balance = $user->balance;
                }
            }else{ //取消订单
                $order->status = 6;
                $trans->status = 4;
            }
            $trans->paytype = $data['paytype'];
            $trans->transtype = $data['transtype'];
            DB::transaction(function()use($user,$order,$trans){
                $user->save();
                $order->save();
                $trans->save();
            },5);
            return [
                'code'=>0,
                'data'=>''
            ];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }

    }

    //通过预定订单ID，计算所有未付款金额(不包含订金)
    protected function getTransMoney($id){
        $prods = (new DiningReserveProd())->where([
            ['did','=',$id],['status','=',2]
        ])->get(); //查询所有待付款的商品
        $yinfu = 0;
        foreach ($prods as $prod){
            $yinfu += $prod->pronum * $prod->proprice;
        }
        return $yinfu;
    }

    //后台餐厅订单退订
    public function dinOrdRefund($data){
        try {
            $order = DiningReserve::find($data['did']);
            if(!$order){
                return [
                    'code'=>1,
                    'message'=>'订单不存在'
                ];
            }
            $user = User::find($order->uid);
            if($user->deleted_at){
                return [
                    'code'=>1,
                    'message'=>'用户被拉黑，不可退订'
                ];
            }
            $order->status = 5;
            $paytype = isset($data['paytype'])?$data['paytype']:1;
            if($paytype==1){ //退款到钱包
                $user->balance += $data['number'];
            }
            $randstr = Str::random(4);
            $transNo = date('YmdHis').$randstr;
            $now = date('Y-m-d H:i:s');
            $transition = [
                'number'=>$data['number'],
                'transtype'=>9,
                'transname'=>'餐桌退订',
                'paytype'=>0,
                'transno'=>$transNo,
                'balance'=>$user->balance,
                'cycle_balance'=>$user->cycle_balance,
                'uid'=>$user->id,
                'orderid'=>$data['did'],
                'status'=>2,
                'created_at'=>$now,
                'updated_at'=>$now
            ];
            $that = $this;
            DB::transaction(function()use ($that,$transition,$order,$user,$now){
                $that->insertGetId($transition);
                $order->save();
                $user->save();
                (new DiningReserveProd())->where('did',$order->id)->whereIn('status',[3,5])->update(['status'=>4,'updated_at'=>$now]);
            },5);
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //后台商品订单支付或取消订单操作
    public function orderPay($data){
        try {
            $order = ProdOrder::find($data['id']);
            if(!$order){
                return [
                    'code'=>1,
                    'message'=>'订单不存在'
                ];
            }
            $user = User::find($order->uid);
            if($user->deleted_at){
                return [
                    'code'=>1,
                    'message'=>'用户被拉黑，不可交易'
                ];
            }
            $trans = $this->where([ ['orderid','=',$data['id']],['transtype','=',3] ])->first();
            if(!$trans){
                return [
                    'code'=>1,
                    'message'=>'交易单不存在'
                ];
            }
            if($trans->status!=1){ //如果该交易单不是待交易状态，则不可确认或取消支付
                return [
                    'code'=>1,
                    'message'=>'该交易单不是待交易状态'
                ];
            }
            if($data['dotype']==2){ //取消订单
                $order->status = 5;
                $trans->status = 4;
            }else{ //确认付款
                $order->status = 2;
                $trans->status = 2;
                $order->paytime = date('Y-m-d H:i:s');
                $trans->paytype = $data['paytype'];
            }
            $trans->balance = $user->balance;
            $trans->cycle_balance = $user->cycle_balance;
            DB::transaction(function()use($user,$order,$trans){
                $order->save();
                $trans->save();
            },5);
            return [
                'code'=>0,
                'data'=>''
            ];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }

    }

    //订单退订
    public function orderRefund($data){
        try {
            $order = ProdOrder::find($data['id']);
            if(!$order){
                return [
                    'code'=>1,
                    'message'=>'订单不存在'
                ];
            }
            $user = User::find($order->uid);
            if($user->deleted_at){
                return [
                    'code'=>1,
                    'message'=>'用户被拉黑，不可交易'
                ];
            }
            $trans = $this->where([ ['orderid','=',$data['id']],['transtype','=',3],['status','=',2] ])->first();
            if(!$trans){
                return [
                    'code'=>1,
                    'message'=>'交易单异常'
                ];
            }
            $user->balance +=  $data['number'];
            $order->status = 6;
            $randstr = Str::random(4);
            $transNo = date('YmdHis').$randstr;
            $this->number = $data['number'];
            $this->transtype = 9;
            $this->paytype = 0;
            $this->transno = $transNo;
            $this->transname = '商品订单退订';
            $this->balance = $user->balance;
            $this->cycle_balance = $user->cycle_balance;
            $this->uid = $order->uid;
            $this->orderid = $data['id'];
            $this->status = 2;
            $that = $this;
            DB::transaction(function ()use ($that,$user,$order){
                $that->save();
                $user->save();
                $order->save();
            },5);
            return [
                'code'=>0,
                'data'=>''
            ];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }

    }

    //后台用户充值
    public function rechargeAdmin($data,$lang="zn"){
        try {
            $user = User::find($data['id']);
            if($user->deleted_at){
                return [
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ];
            }
            $user->balance += $data['number'];
            //充值记录
            $model = new  Recharge();
            $model->number = $data['number'];
            $model->transtatus = $data['transtatus'];
            $model->evidence = $data['evidence'];
            $model->toname = $data['toname'];
            $model->paybank = $data['paybank'];
            $model->bankcard = $data['bankcard'];
            $model->uid = $data['id'];
            //交易记录
            $randstr = Str::random(4);
            $transNo = date('YmdHis').$randstr;
            $this->number = $data['number'];
            $this->transtype = 4;
            $this->transname = '充值';
            $this->paytype = 0;
            $this->transno = $transNo;
            $this->balance = $user->balance;
            $this->cycle_balance = $user->cycle_balance;
            $this->uid = $data['id'];
            $this->status = 2;
            $that = $this;
            DB::transaction(function ()use ($that,$model,$user){
                $user->save();
                $model->save();
                $that->orderid = $model->id;
                $that->save();
            },5);
            return [
                'code'=>0,
                'data'=>''
            ];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ];
        }
    }

    //后台用户提现
    public function withDraw($data,$lang="zn"){
        try {
            $user = User::find($data['id']);
            if($user->balance < $data['number']){
                return [
                    'code'=>1,
                    'message'=>$this->lang['BALANCE_NOENOUGH'][$lang]
                ];
            }
            if($user->deleted_at){
                return [
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ];
            }
            $user->balance -= $data['number'];
            //交易记录
            $randstr = Str::random(4);
            $transNo = date('YmdHis').$randstr;
            $this->number = $data['number'];
            $this->transtype = 7;
            $this->transname = '提现';
            $this->paytype = 1;
            $this->transno = $transNo;
            $this->balance = $user->balance;
            $this->cycle_balance = $user->cycle_balance;
            $this->uid = $data['id'];
            $this->status = 2;
            $that = $this;
            //提现记录
            $model = new  Withdraw();
            $model->number = $data['number'];
            $model->transtatus = $data['transtatus'];
            $model->evidence = $data['evidence'];
            $model->uid = $data['id'];
            DB::transaction(function ()use ($that,$model,$user){
                $user->save();
                $model->save();
                $that->orderid = $model->id;
                $that->save();
            },5);
            return [
                'code'=>0,
                'data'=>''
            ];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ];
        }
    }

    //形成交易单
    public function transAdd($data,$lang="zn"){
        try {
            //获取用户信息
            $user = User::find($data['uid']);
            $that = $this;
            //获取订单信息
            switch ($data['transtype']){
                case 1:
                    $ordermodel = new DiningReserve();
                    $that ->paytype = $data['paytype'];
                    $that ->transname = '餐桌预定订金';
                    if($data['paytype']==1){ //钱包支付
                        if($user->balance < $data['number']){
                            return [
                                'code'=>1,
                                'message'=>$this->lang['BALANCE_NOENOUGH'][$lang]
                            ];
                        }
                        $that ->balance = $user->balance - $data['number'];
                        $that ->cycle_balance = $user->cycle_balance;
                    }elseif ($data['paytype']==2){ //反馈钱包支付
                        if($user->cycle_balance < $data['number']){
                            return [
                                'code'=>1,
                                'message'=>$this->lang['CYCLE_BALANCE_NOENOUGH'][$lang]
                            ];
                        }
                        $that ->balance = $user->balance;
                        $that ->cycle_balance = $user->cycle_balance - $data['number'];
                    }else{
                        $that ->balance = $user->balance;
                        $that ->cycle_balance = $user->cycle_balance;
                    }
                    break;
                case 2:
                    $ordermodel = new DiningReserve();
                    $that ->paytype = $data['paytype'];
                    $that ->transname = '用餐结账';
                    if($data['paytype']==1){ //钱包支付
                        if($user->balance < $data['number']){
                            return [
                                'code'=>1,
                                'message'=>$this->lang['BALANCE_NOENOUGH'][$lang]
                            ];
                        }
                        $that ->balance = $user->balance - $data['number'];
                        $that ->cycle_balance = $user->cycle_balance;
                    }elseif ($data['paytype']==2){ //反馈钱包支付
                        if($user->cycle_balance < $data['number']){
                            return [
                                'code'=>1,
                                'message'=>$this->lang['CYCLE_BALANCE_NOENOUGH'][$lang]
                            ];
                        }
                        $that ->balance = $user->balance;
                        $that ->cycle_balance = $user->cycle_balance - $data['number'];
                    }else{
                        $that ->balance = $user->balance;
                        $that ->cycle_balance = $user->cycle_balance;
                    }
                    break;
                case 3:
                    $ordermodel = new ProdOrder();
                    $that ->transname = '商品购买付款';
                    $that ->paytype = $data['paytype'];
                    if($data['paytype']==1){ //钱包支付
                        if($user->balance < $data['number']){
                            return [
                                'code'=>1,
                                'message'=>$this->lang['BALANCE_NOENOUGH'][$lang]
                            ];
                        }
                        $that ->balance = $user->balance - $data['number'];
                        $that ->cycle_balance = $user->cycle_balance;
                    }elseif ($data['paytype']==2){ //反馈钱包支付
                        if($user->cycle_balance < $data['number']){
                            return [
                                'code'=>1,
                                'message'=>$this->lang['CYCLE_BALANCE_NOENOUGH'][$lang]
                            ];
                        }
                        $that ->balance = $user->balance;
                        $that ->cycle_balance = $user->cycle_balance - $data['number'];
                    }else{
                        $that ->balance = $user->balance;
                        $that ->cycle_balance = $user->cycle_balance;
                    }
                    break;
                case 4: //充值
                    $ordermodel = new Recharge();
                    $that ->paytype = 0;
                    $that ->transname = '充值';
                    $that ->balance = $user->balance + $data['number'];
                    $that ->cycle_balance = $user->cycle_balance;
                    break;
                case 5: //反馈转钱包
                    $ordermodel = "";
                    $that ->paytype = 2;
                    $that ->transname = '反馈钱包转余额';
                    if($user->cycle_balance < $data['number']){
                        return [
                            'code'=>1,
                            'message'=>$this->lang['CYCLE_BALANCE_NOENOUGH'][$lang]
                        ];
                    }
                    $that ->balance = $user->balance + $data['number'];
                    $that ->cycle_balance = $user->cycle_balance - $data['number'];
                    break;
                case 6: //反馈
                    $ordermodel = "";
                    $that ->paytype = 0;
                    $that ->transname = '消费反馈到反馈钱包';
                    $that ->balance = $user->balance;
                    $that ->cycle_balance = $user->cycle_balance + $data['number'];
                    break;
                case 7: //提现
                    $ordermodel = new Withdraw();
                    $that ->paytype = 1;
                    $that ->transname = '提现';
                    if($user->balance < $data['number']){
                        return [
                            'code'=>1,
                            'message'=>$this->lang['BALANCE_NOENOUGH'][$lang]
                        ];
                    }
                    $that ->balance = $user->balance - $data['number'];
                    $that ->cycle_balance = $user->cycle_balance;
                    break;
                case 8: //佣金
                    $ordermodel = new RakeBack();
                    $that ->paytype = 0;
                    $that ->transname = '推广佣金';
                    $that ->balance = $user->balance + $data['number'];
                    $that ->cycle_balance = $user->cycle_balance;
                    break;
                case 9://退款
                    $ordermodel = "";
                    $that ->paytype = 1;
                    $that ->transname = '订单退款';
                    $that ->balance = $user->balance + $data['number'];
                    $that ->cycle_balance = $user->cycle_balance;
                    break;
                default:
                    return [
                        'code'=>1,
                        'message'=>$this->lang['TRANSTYPE_ERR'][$lang]
                    ];
            }
            if($ordermodel){ //存在订单模型
                $order = $ordermodel::find($data['orderid']);
                if(!$order){
                    return [
                        'code'=>1,
                        'message'=>$this->lang['ORDER_UNKOWN'][$lang]
                    ];
                }
            }
            //形成交易单数据
            $randstr = Str::random(4);
            $transNo = date('YmdHis').$randstr;
            $that ->uid = $data['uid'];
            $that ->number = $data['number'];
            $that ->transtype = $data['transtype'];
            $that ->transno = $transNo;
            $that ->orderid = $data['orderid'];
            $that ->status = 1;
            $res = $that->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>$transNo
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>$this->lang['TRANS_CRE_FAIL'][$lang]
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()// $this->lang['ERROR'][$lang]
            ];
        }
    }

    //交易完成
    public function transComplete($data,$user,$lang="zn"){
        DB::beginTransaction();
        try{
            $transition = $this->where('transno',$data['transno'])->first();
            //比较交易数据是否一致
            $check = $this->checkTransData($data,$transition,$lang);
            if($check['code']){
                return $check;
            }
            $now = date('Y-m-d H:i:s');
            $total = $user->total;
            //交易、用户余额修改,订单、订单商品状态修改
            switch ($data['transtype']){
                case 1: //餐桌预定订金支付
                    $ordermodel = (new DiningReserve())->where('id',$transition->orderid)->first();
                    if($data['paytype']==1){ //钱包支付
                        if($user->balance < $data['number']){
                            return [
                                'code'=>1,
                                'message'=>$this->lang['BALANCE_NOENOUGH'][$lang]
                            ];
                        }
                        $transition ->balance = $user->balance - $data['number'];
                        $transition ->cycle_balance = $user->cycle_balance;
                        $user->total = $total + $data['number'];
                    }elseif ($data['paytype']==2){ //反馈钱包支付
                        if($user->cycle_balance < $data['number']){
                            return [
                                'code'=>1,
                                'message'=>$this->lang['CYCLE_BALANCE_NOENOUGH'][$lang]
                            ];
                        }
                        $transition ->balance = $user->balance;
                        $transition ->cycle_balance = $user->cycle_balance - $data['number'];
                    }else{
                        return [
                            'code'=>1,
                            'message'=>$this->lang['PAY_TYPE_ERR'][$lang]
                        ];
                    }
                    $ordermodel->status = 1;
                    $transition->status = 2;
                    //修改订单商品状态
                    (new DiningReserveProd())->where('did',$ordermodel->id)->update(['status'=>1,'updated_at'=>$now]);
                    break;
                case 3: //商品订单付款
                    $ordermodel = (new ProdOrder())->where('id',$transition->orderid)->first();
                    if($data['paytype']==1){ //钱包支付
                        if($user->balance < $data['number']){
                            return [
                                'code'=>1,
                                'message'=>$this->lang['BALANCE_NOENOUGH'][$lang]
                            ];
                        }
                        $transition ->balance = $user->balance - $data['number'];
                        $transition ->cycle_balance = $user->cycle_balance;
                        $user->total = $total + $data['number'];
                    }elseif ($data['paytype']==2){ //反馈钱包支付
                        if($user->cycle_balance < $data['number']){
                            return [
                                'code'=>1,
                                'message'=>$this->lang['CYCLE_BALANCE_NOENOUGH'][$lang]
                            ];
                        }
                        $transition ->balance = $user->balance;
                        $transition ->cycle_balance = $user->cycle_balance - $data['number'];
                    }else{
                        return [
                            'code'=>1,
                            'message'=>$this->lang['PAY_TYPE_ERR'][$lang]
                        ];
                    }
                    $ordermodel->status = 2;
                    $ordermodel->paytime = $now;
                    $transition->status = 2;
                    break;
                case 4: //充值
                    $ordermodel = (new Recharge())->where('id',$transition->orderid)->first();
                    $transition ->balance = $user->balance;
                    $transition ->cycle_balance = $user->cycle_balance;
                    $ordermodel->transtatus = 1;
                    $transition->status = 3;
                    break;
                case 5: //反馈转钱包
                    $ordermodel = "";
                    if($user->cycle_balance < $data['number']){
                        return [
                            'code'=>1,
                            'message'=>$this->lang['CYCLE_BALANCE_NOENOUGH'][$lang]
                        ];
                    }
                    $transition ->balance = $user->balance + $data['number'];
                    $transition ->cycle_balance = $user->cycle_balance - $data['number'];
                    $transition->status = 2;
                    break;
                case 7: //提现
                    $ordermodel = (new Withdraw())->where('id',$transition->orderid)->first();
                    if($user->balance < $data['number']){
                        return [
                            'code'=>1,
                            'message'=>$this->lang['BALANCE_NOENOUGH'][$lang]
                        ];
                    }
                    $transition ->balance = $user->balance - $data['number'];
                    $transition ->cycle_balance = $user->cycle_balance;
                    $ordermodel->transtatus = 1;
                    $transition->status = 3;
                    break;
                default:
                    return [
                        'code'=>1,
                        'message'=>$this->lang['TRANSTYPE_ERR'][$lang]
                    ];
            }
            if($data['transtype']!=4){ //充值时，不改变余额，须有后台审核
                $user->balance = $transition ->balance;
                $user->cycle_balance = $transition ->cycle_balance;
            }
            //用户钱包反馈、积分反馈更改
            if($data['paytype']==1){
                $levinfo = (new UserLevel())->where([
                    ['min','<=',$total],['max','>',$total]
                ])->select('id','cycle_rate','integral')->first()->toArray();
                if(empty($levinfo)){
                    $levinfo = [
                        'id'=>0,
                        'cycle_rate'=>0,
                        'integral'=>0
                    ];
                }
                //形成反馈交易单
                $cycle = intval($data['number'] * $levinfo['cycle_rate'] / 10000 );
                if($cycle>0){
                    $randstr = Str::random(4);
                    $cycle_transNo = date('YmdHis').$randstr;
                    $cycleTrans = new self();
                    $cycleTrans->number = $cycle;
                    $cycleTrans->transtype = 6;
                    $cycleTrans->paytype = 0;
                    $cycleTrans->transname = '消费反馈';
                    $cycleTrans->transno = $cycle_transNo;
                    $cycleTrans->balance = $user->balance;
                    $cycleTrans->cycle_balance = $user->cycle_balance + $cycle;
                    $cycleTrans->uid = $user->id;
                    $cycleTrans->orderid = 0;
                    $cycleTrans->status = 2;
                    $cycleTrans->save();
                    $user->cycle_balance += $cycle;//加上反馈金额
                }
                //计算积分反馈数
                $integral = intval($data['number'] /100 * $levinfo['integral'] / 10000);
                //形成反馈积分记录
                if($integral>0){
                    $randstr = Str::random(4);
                    $integral_transNo = date('YmdHis').$randstr;
                    $integralTrans = new JifenDetail();
                    $integralTrans->number = $cycle;
                    $integralTrans->transtype = 2;
                    $integralTrans->transname = '消费积分反馈';
                    $integralTrans->transno = $integral_transNo;
                    $integralTrans->coin = $user->coin + $integral;
                    $integralTrans->uid = $user->id;
                    $integralTrans->orderid = 0;
                    $integralTrans->status = 2;
                    $integralTrans->save();
                    $user->coin += $integral;//加上反馈积分
                }
                //查找推广人
                $spreadid = (new SpreadRecord())->where('uid',$user->id)->value('spreadid');
                //推荐人首单返佣金
                if($spreadid){
                    $backid = (new RakeBack())->where([
                        ['spreadid','=',$spreadid],['uid','=',$user->id],['status','=',1]
                    ])->value('id');
                    if(!$backid && in_array($data['transtype'],[1,3])){ //判断有无佣金反馈记录且是消费
                        $spreduser = (new User())->where('spreadid',$spreadid)->first();
                        if($spreduser && !$spreduser->deleted_at){
                            $spredRate = (new UserLevel())->where([ ['min','<=',$spreduser->total],['max','>',$spreduser->total] ])->value('spred_rate');
                            if($spredRate>0){
                                $spredback = intval($data['number'] * $spredRate / 10000);
                                $ordtype = $data['transtype']==1?1:2;
                                $total = $ordermodel->total?$ordermodel->total:($ordermodel->pronum * $ordermodel->proprice - $ordermodel->youhui);
                                //形成返佣记录
                                $backid =(new RakeBack())->insertGetId([
                                    'spreadid'=>$spreadid,
                                    'ordertype'=>$ordtype,
                                    'uid'=>$user->id,
                                    'orderid'=>$transition->orderid,
                                    'total'=>$total,
                                    'number'=>$spredback,
                                    'status'=>1,
                                    'created_at'=>$now,
                                    'updated_at'=>$now
                                ]);
                                //形成返佣交易单
                                $backTrans = new self();
                                $randstr = Str::random(4);
                                $back_transNo = date('YmdHis').$randstr;
                                $backTrans->number = $spredback;
                                $backTrans->paytype = 0;
                                $backTrans->transtype = 8;
                                $backTrans->transname = '推广返佣';
                                $backTrans->transno = $back_transNo;
                                $backTrans->balance = $spreduser->balance + $spredback;
                                $backTrans->cycle_balance = $spreduser->cycle_balance;
                                $backTrans->uid = $spreduser->id;
                                $backTrans->orderid = $backid;
                                $backTrans->status = 2;
                                $backTrans->save();
                                $spreduser->balance += $spredback;//加上返佣金额
                                $spreduser->save();
                            }
                        }
                    }
                }
            }
            $user->save();
            if($ordermodel) {
                $ordermodel->save();
            }
            $transition->save();
            DB::commit();
            return [
                'code'=>0,
                'data'=>[
                    'transno'=>$data['transno'],
                    'number'=>$data['number']
                ]
            ];
        }catch (\Exception $e){
            DB::rollBack();
            return [
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ];
        }
    }

    //比较交易数据是否一致
    protected function checkTransData($transdata,$transition,$lang="zn"){
        try{
            if($transdata['number']!=$transition->number){
                return ['code'=>1,'message'=>$this->lang['TRANS_NUM_ERR'][$lang]];
            }
            if($transdata['transtype']!=$transition->transtype){
                return ['code'=>1,'message'=>$this->lang['TRANS_TYPE_ERR'][$lang]];
            }
            if($transdata['paytype']!=$transition->paytype){
                return ['code'=>1,'message'=>$this->lang['TRANS_PAY_ERR'][$lang]];
            }
            return ['code'=>0,'data'=>true];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ];
        }
    }
}
